﻿/******************************************************************************
* File description: ExperimentLog.qml
* Function: 实验日志
* Current Version: 1.0.0
* Author: JNN
* Date: 2025-07-01
*  -----
*  Copyright by 博坤生物 .
 *****************************************************************************/
import QtQuick 2.0
import QtQuick.Layouts 1.12
import QtQuick.Controls 2.12
Rectangle {
    id: root
    anchors.fill: parent
    anchors.leftMargin: 20*dp
    anchors.rightMargin: 20*dp
    anchors.topMargin: 10*dp
    anchors.bottomMargin: 10*dp
    property bool visibleTitle: true

    property var listData: [{
            "FinishTime": "0",
            "Time": "0",
            "Step": "0",
            "Status": "进行中",
            "ActionInfo": "1"
        }]

    property int lastStep: 0        // 用于记录看需不需要给干燥/冷却刷新时间

    /** @brief 设置数据 */
    function setData(data){
        listData = data
        for(let i = data.length-1; i >= 0; i --){
            if(data[i]["Status"]==="进行中"){
                // 滚动条跳转到进行中的那一步
                listView.contentY = Math.max(0, Math.min((i-1)*40*dp, data.length*40*dp-listView.height)) + listView.originY
                // 看需不需要给干燥和冷却计时

                if(data[i]["ActionInfo"].toString().indexOf("干燥")!==-1){

                    if(lastStep !== gStateExp.expStep){
                        gStateExp.expAirPumpTime = Number(data[i]["ActionInfo"].replace("干燥", "").replace("分钟", "")) * 60
                    }
                    gIsAirPumping = true
                }else{
                    gIsAirPumping = false
                    gStateExp.expAirPumpTime = 0
                }
                if(data[i]["ActionInfo"].toString().indexOf("冷却")!==-1){
                    // 放弃冷却的计时了
//                    if(lastStep !== gStateExp.expStep){
//                        gStateExp.expWaterPumpTime = Number(data[i]["ActionInfo"].replace("冷却", "").replace("分钟", "")) * 60
//                    }
//                    gIsWaterPumping = true
                }else{
                    gIsWaterPumping = false
                    gStateExp.expWaterPumpTime = 0
                }

                lastStep = gStateExp.expStep        // 记录当前进行中的步骤
//                break                             // 实验可能会有多个进行中，所以就不break了
            }
        }
    }


    property var lineScale: [0.1, 0.2, 0.4, 0.1, 0.2]

    // 标题
    Rectangle{
        height: 40*dp
        width: root.width
        x: 2*dp
        visible: visibleTitle
        Text{
            text: "实验日志"
            font.bold: true
            font.pixelSize: config.fontsize_1
            anchors.verticalCenter: parent.verticalCenter
        }
    }

    // 表头
    Rectangle{
        y: 45*dp
        height: 40*dp; width: root.width
        color: "#E6E6E6"
        Rectangle{              // 表格线
            color: "white"
            height: parent.height - 2
            width: parent.width
            y: 1; x: 0
        }
        Row{
            height: parent.height; width: parent.width
            Item{
                height: parent.height
                width: parent.width * lineScale[0]
                Text{
                    text: "步骤"
                    anchors.centerIn: parent
                    color: "#888888"
                    font.pixelSize: config.fontsize
                }
            }
            Item{
                height: parent.height
                width: parent.width * lineScale[1]
                Text{
                    text: "时间"
                    anchors.centerIn: parent
                    color: "#888888"
                    font.pixelSize: config.fontsize
                }
            }Item{
                height: parent.height
                width: parent.width * lineScale[2]
                Text{
                    text: "动作信息"
                    anchors.centerIn: parent
                    color: "#888888"
                    font.pixelSize: config.fontsize
                    wrapMode: Text.WordWrap
                }
            }
            Item{
                height: parent.height
                width: parent.width * lineScale[3]
                Text{
                    text: "用时(ms)"
                    anchors.centerIn: parent
                    color: "#888888"
                    font.pixelSize: config.fontsize
                }
            }
            Item{
                height: parent.height
                width: parent.width * lineScale[4]
                Text{
                    text: "状态"
                    anchors.centerIn: parent
                    color: "#888888"
                    font.pixelSize: config.fontsize
                }
            }
        }
    }

    ListView{
        id:listView
        y: 85*dp
        height: root.height - 85*dp
        width: root.width
        model: listData.length
        delegate: logDelegate
        clip: true
    }

    Component{
        id: logDelegate
        Rectangle{
            height: 40*dp
            width: root.width
            Rectangle{
                height: 1
                width: root.width
                color: "#E6E6E6"
                y: parent.height - 1
            }
            Row{
                height: parent.height; width: parent.width
                Item{
                    height: parent.height
                    width: parent.width*lineScale[0]
                    Text{
                        text: listData[index]["Step"]
                        anchors.centerIn: parent
                        font.pixelSize: config.fontsize
                    }
                }Item{
                    height: parent.height
                    width: parent.width*lineScale[1]
                    Text{
                        text: listData[index]["FinishTime"]
                        anchors.centerIn: parent
                        font.pixelSize: config.fontsize
                    }
                }Item{
                    height: parent.height
                    width: parent.width*lineScale[2]
                    Text{
                        text: listData[index]["ActionInfo"]
                        font.pixelSize: config.fontsize
                        anchors.fill: parent
                        horizontalAlignment: Text.AlignHCenter
                        verticalAlignment: Text.AlignVCenter
                        wrapMode: Text.WrapAnywhere
                    }
                }Item{
                    height: parent.height
                    width: parent.width*lineScale[3]
                    Text{
                        text: listData[index]["Time"]
                        anchors.centerIn: parent
                        font.pixelSize: config.fontsize
                    }
                }Item{
                    height: parent.height
                    width: parent.width*lineScale[4]
                    Rectangle{
                        height: 28*dp; width: 80*dp
                        anchors.centerIn: parent
                        radius: 15*dp
                        border.color: {
                            if(listData[index]["Status"] === "完成")      return  "#67C23A"
                            else if(listData[index]["Status"] === "异常") return  "#F56C6C"
                            else if(listData[index]["Status"] === "进行中") return  "#ECB311"
                            else if(listData[index]["Status"] === "检测中") return  "#ECB311"
                            else                     return   "#999999"
                        }
                        color: {
                            if(listData[index]["Status"] === "完成")      return  "#F0F9EB"
                            else if(listData[index]["Status"] === "异常") return  "#FBEAEA"
                            else if(listData[index]["Status"] === "进行中") return  "#FDF3EB"
                            else if(listData[index]["Status"] === "检测中") return  "#FDF3EB"
                            else                     return   "#F0F0F0"
                        }
                    }

                    Text{
                        text: listData[index]["Status"]
                        anchors.centerIn: parent
                        font.pixelSize: config.fontsize
                        color: {
                            if(text === "完成")         return "#67C23A"
                            else if(text === "异常")    return "#F56C6C"
                            else if(text === "进行中")    return "#ECB311"
                            else if(text === "检测中")    return "#ECB311"
                            else                       return "#999999"
                        }
                    }
                }
            }
        }
    }


}
